﻿using System;
using System.Collections.Generic;
using Demo.Services.View.Controllers;
using Demo.CommonBase.Logging;
using Demo.CommonBase.View.Forms;
using Demo.Services.Contracts.DTO.Infos;
using System.Reflection;
using Demo.CommonBase.Exceptions;
using Ext.Net;
using Demo.Services.Services.Impl.Managers;

namespace Demo.Web.Catalogs
{
    public partial class ResourceEdit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            {
                int id = 0;
                LoadResourceType();
                if (Request["Id"] != null)
                {
                    id = Convert.ToInt32(Request["Id"]);
                    SetEditData(id);
                }
                else
                {
                    Session["Assigned"] = null;
                    LoadTecnologies(id);
                }
               
            }
        }
        private void LoadResourceType()
        {
            var resourceTypeManager = new ResourceTypeManager();
            List<ResourceTypeInfo> resourceTypeList = resourceTypeManager.GetList();
            foreach (ResourceTypeInfo type in resourceTypeList)
            {
                cboResourceType.Items.Add(new ListItem { Value = type.Id.ToString(), Text = type.Description });
            }           
        }

        private void LoadTecnologies(int id)
        {
            ResourceController controller;
            List<TecnologyInfo> assignedTecnologyList;
            List<TecnologyInfo> unAssignedTecnologyList;
            try
            {
                Logger.Info();
                SetValue("ResourceId", id);
                controller = new ResourceController();
                controller.ExecuteService(this, "Services.GetAssignedTecnologies");
                assignedTecnologyList = (List<TecnologyInfo>)GetValue("AsignedTecnologyList");
                if (assignedTecnologyList != null && assignedTecnologyList.Count > 0)
                {
                    Session["Assigned"] = assignedTecnologyList;
                }
                else
                {
                    Session["Assigned"] = null;
                }
                controller.ExecuteService(this, "Services.GetUnAssignedTecnologies");
                unAssignedTecnologyList = (List<TecnologyInfo>)GetValue("UnAsignedTecnologyList");
                if (unAssignedTecnologyList != null && unAssignedTecnologyList.Count > 0)
                {
                    Session["UnAssigned"] = unAssignedTecnologyList;
                }
                else
                {
                    Session["UnAssigned"] = null;
                }                
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }           
        }
        private void SetEditData(int id)
        {            
            ResourceController controller;
            ResourceInfo resourceInfo;
            
            try
            {
                Logger.Info();                               
                
                SetValue("Id", id);
                controller = new ResourceController();
                controller.ExecuteService(this, "Services.GetResourceById");
                resourceInfo = (ResourceInfo)GetValue("ResourceInfo");
                txtResourceID.Text = resourceInfo.Id.ToString();
                txtName.Text = resourceInfo.Name;
                txtLastName.Text = resourceInfo.LastName;
                txtSecondLastName.Text = resourceInfo.SecondLastName;
                txtBirthDate.SetValue(resourceInfo.BirthDate);
                txtPhoneNumber.Text = resourceInfo.PhoneNumber;
                cboResourceType.Select(resourceInfo.ResourceType.ToString());                
                LoadTecnologies(id);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }            
        }       
        protected void Asignar(object sender, DirectEventArgs e)
        {
            WindowTenology.ShowWindow();
        }

        protected void Guardar(object sender, DirectEventArgs e)
        {
            ResourceController controller;
            ResourceInfo resourceInfo;
            try
            {
                resourceInfo = GetData();
                SetValue("ResourceInfo", resourceInfo);
                controller = new ResourceController();
                controller.ExecuteService(this, "Services.SaveResource");
                ExtNet.Js.AddScript("parentAutoLoadControl.hide();");
                ExtNet.Js.AddScript("parent.Ext.getCmp('btnRefresh').fireEvent('click');");
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }    

        }
        private ResourceInfo GetData()
        {
            var resource = new ResourceInfo();
            resource.Id = txtResourceID.Text == string.Empty ? 0 : Convert.ToInt32(txtResourceID.Text);
            resource.Name = txtName.Text;
            resource.LastName = txtLastName.Text;
            resource.SecondLastName = txtSecondLastName.Text;
            resource.ResourceType = Convert.ToInt32(cboResourceType.SelectedItem.Value);
            resource.BirthDate = txtBirthDate.SelectedValue == null ? string.Empty : GetFormatDate(txtBirthDate.SelectedValue.ToString());
            resource.PhoneNumber = txtPhoneNumber.Text;
            if (Session["Assigned"] != null)
            {
                var tecnologyList = (List<TecnologyInfo>)Session["Assigned"];
                resource.TecnologyList = tecnologyList;
            }
            return resource;
        }
    }
}